﻿using Coravel;
using Coravel.Invocable;

namespace WebAdmin;

public static class AppWeb
{
    public static void AddApplication(this IServiceCollection services, Action<CoreOption> action)
    {
#if DEBUG
        Config.IsDebug = true;
#endif
        services.AddKnownCMSAdmin();
        services.AddKnownCells();
        services.AddKnownWeb(action);

        services.AddScoped<IHomeService, HomeService>();

        //添加定时任务
        services.AddScheduler();
        services.AddTransient<ImportTaskJob>();
    }

    public static void UseApplication(this WebApplication app)
    {
        //使用Known框架
        app.UseKnown();
        //配置定时任务
        app.Services.UseScheduler(scheduler =>
        {
            //每5秒执行一次异步导入
            scheduler.Schedule<ImportTaskJob>().EveryFiveSeconds();
        });
    }
}

class ImportTaskJob : IInvocable
{
    public Task Invoke() => ImportHelper.ExecuteAsync();
}